﻿using System.Windows.Controls;
using System.Xml.Linq;
using System.Windows.Data;
using Acando.Dynamics.CRM.Silverlight.Library.Converter;
using System;
using System.Windows;
//using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using Acando.Dynamics.CRM.Silverlight.Library.MetaSdk;

namespace Acando.Dynamics.CRM.Silverlight.Library.Controls.FieldControls
{
    public partial class CrmDateTimeControl : BaseValueControl
    {
        private bool updateByControls = false;
        private bool updateByData = false;
        private bool isDateOnly = false;

        public DateTime? Value
        {
            get { return (DateTime?)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(DateTime?), typeof(CrmDateTimeControl), new PropertyMetadata(new PropertyChangedCallback(ValueChangedCallback)));

        private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CrmDateTimeControl c = (CrmDateTimeControl)d;

            if (!c.updateByControls)
            {

                c.updateByData = true;
                c.ctrlDate.SelectedDateChanged -= new EventHandler<SelectionChangedEventArgs>(c.ctrlDate_SelectedDateChanged);
                //c.ctrlTime.SelectedTimeChanged -= new EventHandler(c.ctrlTime_SelectedTimeChanged);

                //set date auf date control
                if (c.Value == null)
                    c.ctrlDate.SelectedDate = null;
                else
                    c.ctrlDate.SelectedDate = c.Value.Value.Date;
                
                ////set time auf time control
                //if (!c.isDateOnly)
                //{
                //    if (c.Value.HasValue)
                //        c.ctrlTime.SelectedTime = c.Value.Value.TimeOfDay;
                //    else
                //        c.ctrlTime.SelectedTime = new TimeSpan();
                //}

                c.updateByData = false;


                c.ctrlDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(c.ctrlDate_SelectedDateChanged);
                //c.ctrlTime.SelectedTimeChanged += new EventHandler(c.ctrlTime_SelectedTimeChanged);
            }
        }

        // constructor
        public CrmDateTimeControl(IRecordControl recordControl, string controlType, string fieldName, DateTimeAttributeMetadata attrMeta)
            : base(recordControl, controlType, fieldName)
        {
            InitializeComponent();

            if (attrMeta.Format.Value == DateTimeFormat.DateOnly)
                this.isDateOnly = true;

            this.ctrlDate.IsEnabled = !recordControl.IsReadOnly;
            //this.ctrlTime.IsReadOnly = recordControl.IsReadOnly;

            //if (this.isDateOnly)
            //    this.ctrlTime.Visibility = Visibility.Collapsed;

            this.SetBinding(CrmDateTimeControl.ValueProperty, this.GetBinding(recordControl, controlType, fieldName));
        }

        private void ctrlDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!this.updateByData)
                this.RetrieveValueFromControls();
        }

        private void ctrlTime_SelectedTimeChanged(object sender, EventArgs e)
        {
            if (!this.updateByData)
                this.RetrieveValueFromControls();
        }

        //private void ctrlTime_Loaded(object sender, RoutedEventArgs e)
        //{
        //    this.SetBinding(CrmDateTimeControl.ValueProperty, new Binding(attributeName) { Mode = BindingMode.TwoWay, Converter = new CrmDateTimeConverter() });
        //}

        private void RetrieveValueFromControls()
        {
            this.updateByControls = true;

            if (ctrlDate.SelectedDate.HasValue)
                this.Value = ctrlDate.SelectedDate.Value.Date;
            else
                this.Value = new DateTime?();

            //if (!this.isDateOnly)
            //    this.Value = this.Value.Value.Add(ctrlTime.SelectedTime);
          
            this.updateByControls = false;
        }

     
    }
}